package com.wyf.order.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.wyf.order.service.OrderService;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Resource
    OrderService orderService;

    @RequestMapping("/add")
    public String add() {
        System.out.println("下单成功");// String msg = restTemplate.getForObject("http://localhost:8011/stock/reduck", String.class);
        return "生成订单";
    }

    @RequestMapping("/get")
    public String get() {
        return "查询订单";
    }

    @RequestMapping("/flow")
    // @SentinelResource(value = "flow", blockHandler = "flowBlockHandler")
    public String flow() {
        return "正常访问";
    }

    @RequestMapping("/flowThread")
    // @SentinelResource(value = "flowThread", blockHandler = "flowBlockHandler")
    public String flowThread() throws InterruptedException {
        TimeUnit.SECONDS.sleep(5);
        return "正常访问";
    }


    public String flowBlockHandler(BlockException exception) {
        exception.printStackTrace();
        return "流控";
    }

    @RequestMapping("/test1")
    public String test1() {
        return orderService.getUser();
    }

    @RequestMapping("/test2")
    public String test2() {
        return orderService.getUser();
    }
}
